package com.leftheart.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils {


    public static String jsonDate(Object date){
        return jsonDate(date,"yyyy-MM-dd HH:mm;ss");
    }


    public static String jsonDate(Object date, String dateFormat){
        ObjectMapper mapper = new ObjectMapper();
//      不使用时间戳
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
//       自定义日期格式
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat(dateFormat);
        mapper.setDateFormat(simpleDateFormat);
        try {
            return mapper.writeValueAsString(date);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

}
